<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Index extends MY_Controller {
    
    public function __construct()
    {
        parent::__construct();
        $this->load->library('form_validation');
    }
    
    // ------------------------------------------------------------------------
    
    public function index()
    {
        //$this->auth->is_logged_in();
        
        //redirect('dashboard');
		
		//if () echo 'lol' 
		
		//echo SITE_CODE;
		
		$data['title'] = 'VIEWWWWWWWWWW';
		
		$this->load->view('login', $data);
    }
    
    // ------------------------------------------------------------------------
	
	public function asa()
	{
		echo 'asa';
	}
	
	// ------------------------------------------------------------------------
    
    public function login()
    {
        if ($this->input->is_ajax_request())
        {
            $this->form_validation->set_rules('email', '<strong>Email</strong>', 'required|valid_email|exists[users.email]');
            $this->form_validation->set_rules('password', '<strong>Password</strong>', 'required|trim');
            
            if ($this->form_validation->run())
            {
                $post = array(
                    'email'     => $this->input->post('email', TRUE),
                    'password'  => $this->input->post('password', TRUE)
                );
                
                $query = $this->auth->login($post);
                
                if ($query)
                {
                    // redirect somewhere
                    echo json_encode(array('success' => TRUE, 'action' => 'redirect', 'url' => site_url('dashboard')));
                    return TRUE;
                }
                
                echo json_encode(array('success' => FALSE, 'errors' => '<p class="error">The <strong>User Account</strong> is not found.</p>'));
                return TRUE;
            }
            
            echo json_encode(array('success' => FALSE, 'errors' => validation_errors()));
            return TRUE;
        }
        else
        {
            // load view
            $data = array(
                'title'     => 'Login'
            );

            $this->load->view('login', $data);
        }
    }
    
    // ------------------------------------------------------------------------
	
	public function logout()
	{
		$this->auth->logout();
		redirect('login');
	}
	
	// ------------------------------------------------------------------------
}

/* End of file admin.php */
/* Location: ./application/controllers/admin.php */